
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author tkor
 */
public class Menu extends GameCanvas{

    
    private Image background;
    private Image shader;
    

    private Timer mTimer;
    private Graphics g;
    
    private Start start;
    private Settings settings;
    private Exit exit;
    private Hightscore hightscore;
    private Continue continue_;
    public Menudetail[] items;
    private int currentActiveItem = 0;
    public Menu() {
        super(false);
        try {
            ImageLoader loader = ImageLoader.getInstance();
            background = loader.loadImage("/splash_screen.png");
            shader = loader.loadImage("/shader.png");
        }catch(IOException io) {

        }
        setFullScreenMode(true);
        items = new Menudetail[5];
        continue_ = new Continue();
        continue_.setPos(0, 0);
        start = new Start();
        start.setPos(0, 80);
        settings = new Settings();
        settings.setPos(0, 120);
        exit = new Exit();
        exit.setPos(0, 160);
        hightscore = new Hightscore();
        hightscore.setPos(0, 40);
        
        try {
            Class.forName("com.nokia.mid.ui.gestures.GestureInteractiveZone");
//            exititem = new ExitItem(Main.getInstance());
        }catch(ClassNotFoundException e) {
//            exititem = new ExitItem(MainNonTouch.getInstance());
        }

        items[0] = start;
        items[1] = settings;
        items[2] = exit;
        items[3] = hightscore;
        items[4] = continue_;
    }

    public void showNotify() {

        mTimer = new Timer();
        g = getGraphics();
        TimerTask ui = new TimerTask() {
          public void run()  {
              render(g);
              flushGraphics();
          }
        };
        mTimer.schedule(ui, 0, 30);
    }

    public void hideNotify() {
        mTimer.cancel();
    }

    private void render(Graphics g) {
        int anchor = Graphics.TOP|Graphics.LEFT;
        g.drawImage(background, 0, 0, anchor);
        g.drawImage(shader, 0, 0, anchor);

        start.render(g);
        settings.render(g);
        exit.render(g);
        hightscore.render(g);
        continue_.render(g);
    }

    protected void pointerPressed(int x, int y) {
        start.pointerPressed(x, y);
        settings.pointerPressed(x, y);
        exit.pointerPressed(x, y);
        hightscore.pointerPressed(x, y);
        continue_.pointerPressed(x, y);
    }

    protected void pointerReleased(int x, int y) {
        start.pointerReleased(x,y);
        settings.pointerReleased(x,y);
        exit.pointerReleased(x,y);
        hightscore.pointerReleased(x, y);
        continue_.pointerReleased(x, y);
    }

    protected void keyPressed(int keyCode) {
switch(keyCode) {
            case KeyCodes.UP:
                items[currentActiveItem].setInactive();
                if(currentActiveItem == 0) {
                    currentActiveItem = 2;
                }else {
                    currentActiveItem--;
                }
                items[currentActiveItem].setActive();
                break;
            case KeyCodes.DOWN:
                items[currentActiveItem].setInactive();
                if(currentActiveItem == 2) {
                    currentActiveItem = 0;
                }else {
                    currentActiveItem++;
                }
                items[currentActiveItem].setActive();
                break;
            case KeyCodes.MIDDLE_SOFT_KEY:
                items[currentActiveItem].setMenuItem();
                break;
        }

    }



}
